/**
 * 版本列表
 */

var version = new Object({
    table:null,
    init:function(){
        if(null != this.table)
            return;
        this.queryInfo();
        this.bindEvent();
    },
    queryInfo:function(){
        this.table = $('#tb_version').bootstrapTable({
            url: contextPath + '/version/listData.json',
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20,50,100]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 700,
            search: true,
            searchAlign: 'left',
            toolbar: '<div style="float: left;margin-right:5px;">' +
            '    <button class="btn btn-default" type="button" onclick="version.add()">' +
            '        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            '        <span>新增</span>' +
            '    </button>' +
            '</div>',
            idField: 'id',
            uniqueId: 'id',
            //singleSelect: true,
            clickToSelect: true,
            columns: [{
                field: 'versionNo',
                title: '版本号',
                width: 100
            }, {
                field: 'versionName',
                title: '版本名称',
                width: 100
            }, {
                field: 'publishTime',
                title: '发布时间',
                width: 100,
                formatter:function(value,row,index){
                    if(value != null) {
                        return (new Date(value)).Format("yyyy-MM-dd hh:mm:ss");
                    }
                    return "";
                }
            }, {
                field: 'stopTime',
                title: '停用时间',
                width: 100,
                formatter:function(value,row,index){
                    if(value != null) {
                        return (new Date(value)).Format("yyyy-MM-dd hh:mm:ss");
                    }
                    return "";
                }
            }, {
                field: 'status',
                title: '状态',
                width: 100,
                formatter:function(value,row,index){
                    if(value == 1){
                        return '启用';
                    }else{
                        return '停用';
                    }
                }
            },{
                field: 'os',
                title: '手机系统',
                width: 100
            },{
                field: 'op',
                title: '操作',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    var strHtml = '';
                    //strHtml += '<a href="javascript:void(0);" onclick="client_list.edit('+row.id+',\''+row.name+'\','+row.ratio+')">编辑</a>&nbsp;&nbsp;';
                    strHtml = '<div class="btn-group">' +
                        '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">更多<i class="fa fa-caret-down"></i></button>' +
                        '   <ul class="dropdown-menu">'+
                        '       <li><a href="javascript:void(0);" onclick="version.editVersion('+row.id+')">编辑</a></li>'+
                        '   </ul>'+
                        '</div>'
                    return strHtml;
                }
            }]
        });
        $('.search input').attr("placeholder", "版本号|名称");
    },
    bindEvent: function () {
    },
    add:function(){
        var addVersionModal = '<div id="addVersionModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">创建版本</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="addVersionForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">版本号：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="versionNo" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">版本名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="versionName" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">状态：</label>'+
            '                       <div class="col-sm-9" style="padding-top:3px;">'+
            '                           <div class="radio" style="margin-top: 0;float:left;margin-right:20px">'+
            '                               <label>'+
            '                                   <input type="radio" name="status" id="status1" value="0" class="px">'+
            '                                   <span class="lbl">停用</span>'+
            '                               </label>'+
            '                           </div> <!-- / .radio -->'+
            '                           <div class="radio" style="margin-bottom: 0;float:left">'+
            '                               <label>'+
            '                                   <input type="radio" name="status" id="status2" value="1" checked="true" class="px">'+
            '                                   <span class="lbl">启用</span>'+
            '                               </label>'+
            '                            </div>'+
            '                       </div>'+
            '                   </div>' +
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">手机系统：</label>'+
            '                       <div class="col-sm-9" style="padding-top:3px;">'+
            '                           <div class="radio" style="margin-top: 0;float:left;margin-right:20px">'+
            '                               <label>'+
            '                                   <input type="radio" name="os" id="os1" value="ios" class="px">'+
            '                                   <span class="lbl">ios</span>'+
            '                               </label>'+
            '                           </div> <!-- / .radio -->'+
            '                           <div class="radio" style="margin-bottom: 0;float:left">'+
            '                               <label>'+
            '                                   <input type="radio" name="os" id="os2" value="android" checked="true" class="px">'+
            '                                   <span class="lbl">android</span>'+
            '                               </label>'+
            '                            </div>'+
            '                       </div>'+
            '                   </div>' +
            '               </form>'+
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="version.addSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(addVersionModal)[0]);
        $('#addVersionModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#addVersionModal')[0]);
        });
        version.bindEvent();
        //显示
        $("#addVersionModal").modal('show')
    },
    addSubmit:function(){
        $.ajax({
            url:contextPath+'/version/add',
            method:'post',
            data:$('#addVersionForm').serialize(),
            success:function(res){
                version.table.bootstrapTable('refresh');
            },
            error: function () {
                alert_message("添加出错，请联系管理员")
            }
        });
        version.closeAddModal();
    },
    closeAddModal:function(){
        $('#addVersionModal').modal('hide');
    },
    editVersion:function(id){
        var res = "";
        $.ajax({
            url:contextPath+'/version/'+id,
            method:'get',
            async:false,
            success:function(data){
                //获取数据
                res = data;
            },
            error:function(){
                alert_message('请求错误，请联系管理员');
            }
        });
        var editVersionModal = '<div id="editVersionModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">编辑版本</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="editVersionForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">版本号：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="versionNo" readonly="readonly" value="'+res.data.versionNo+'" />'+
            '                           <input type="hidden" class="form-control" name="id" value="'+res.data.id+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">版本名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="versionName" value="'+res.data.versionName+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">状态：</label>'+
            '                       <div class="col-sm-9" style="padding-top:3px;">'+
            '                           <div class="radio" style="margin-top: 0;float:left;margin-right:20px">'+
            '                               <label>';
        if(res.data.status == 0){
            editVersionModal += '<input type="radio" name="status" id="status1" value="0" checked="true"  class="px">';
        }else{
            editVersionModal += '<input type="radio" name="status" id="status1" value="0"  class="px">';
        }
        editVersionModal += '                     <span class="lbl">停用</span>'+
            '                               </label>'+
            '                           </div> <!-- / .radio -->'+
            '                           <div class="radio" style="margin-bottom: 0;float:left">'+
            '                               <label>';
        if(res.data.status == 1){
            editVersionModal +=' <input type="radio" name="status" id="status2" checked="true" value="1" class="px">';
        }else{
            editVersionModal +=' <input type="radio" name="status" id="status2" value="1" class="px">';
        }
        editVersionModal +='                 <span class="lbl">启用</span>'+
            '                               </label>'+
            '                            </div>'+
            '                       </div>'+
            '                   </div>' +
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">手机系统：</label>'+
            '                       <div class="col-sm-9" style="padding-top:3px;">'+
            '                           <div class="radio" style="margin-top: 0;float:left;margin-right:20px">'+
            '                               <label>';
        if(res.data.os == 'ios'){
            editVersionModal += '<input type="radio" name="os" id="os1" value="ios" checked="true"  class="px">';
        }else{
            editVersionModal += '<input type="radio" name="os" id="os1" value="ios"  class="px">';
        }
        editVersionModal += '                     <span class="lbl">ios</span>'+
            '                               </label>'+
            '                           </div> <!-- / .radio -->'+
            '                           <div class="radio" style="margin-bottom: 0;float:left">'+
            '                               <label>';
        if(res.data.os == 'android'){
            editVersionModal +=' <input type="radio" name="os" id="os2" checked="true" value="android" class="px">';
        }else{
            editVersionModal +=' <input type="radio" name="os" id="os2" value="android" class="px">';
        }
        editVersionModal +='                 <span class="lbl">android</span>'+
            '                               </label>'+
            '                            </div>'+
            '                       </div>'+
            '                   </div>' +
            '               </form>'+
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="version.editVersionSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //alert(editVersionModal);

        //显示
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(editVersionModal)[0]);
        $('#editVersionModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#editVersionModal')[0]);
        });

        //绑定事件
        version.bindEvent();

        //显示
        $('#editVersionModal').modal('show');


    },
    editVersionSubmit:function(){
        $.ajax({
            url:contextPath+'/version/update',
            data:$('#editVersionForm').serialize(),
            method:'post',
            success:function(res){
                alert_message("修改成功");
                version.table.bootstrapTable('refresh');
                $('#editVersionModal').modal('hide');
            },
            error:function(){
                alert_message("修改出错，请联系管理员");
            }
        });
    }
});